package com.person.sys.action;

import java.util.List;

import com.person.commons.struts2.BaseAction;
import com.person.sys.model.SysFunc;
import com.person.sys.model.SysMenu;
import com.person.sys.model.SysMenuNode;
import com.person.sys.service.SysFuncService;
import com.person.sys.service.SysMenuService;

public class SysMenuAction extends BaseAction {
	private SysMenuService sysMenuService;
	private SysFuncService sysFuncService;
	private SysMenu sysMenu;
	private SysFunc sysFunc;
	private List<SysMenu> sysMenus;
	private List<SysFunc> sysFuncs;
	private List<SysMenuNode> sysMenuNodes;
	private String menuCode;
	private String menuImage;
	private int flag = 0;
	
	/**
	 * 菜单管理的首页
	 * 左边显示所有的一级菜单
	 * 右边是菜单管理的说明
	 * @return
	 */
	public String index(){
		
		return "index";
	}
	
	public String left(){
		
		sysMenus = sysMenuService.sysMenuList();
		
		
		
		return "left";
	}
	public String top(){
		return "top";
	}
	public String right(){
		
		return "right";
	}
	
	/**
	 * 转向到添加页面
	 * @return
	 */
	public String preCreate(){
		
		return "createMenu";
	}
	
	/**
	 * 添加一级菜单节点
	 * @return
	 */
	public String create(){
		String forward = "";
		char state;
		try {
			state = sysMenuService.createSysMenu(sysMenu);
			
			if(state == '1'){
				info = getText("menu.create.success");
				flag = 1;
				forward = "create_success";
			}else if(state == '2'){
				
				info = getText("menu.code");
				forward = INPUT;
				
			}else if(state == '3'){
				
				info =  getText("menu.name");
				forward = INPUT;
			}else if(state == '4'){
				
				info =  getText("menu.nameEn");
				forward = INPUT;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			forward = INPUT;
		}
		
		
		
		return forward;
	}
	
	/**
	 * 转向到修改页面
	 * @return
	 */
	public String preModify(){
		String forward = "";
		sysMenu = sysMenuService.findSysMenuById(sysMenu.getMenuId());
		
		return "modifyMenu";
	}
	
	/**
	 * 修改一级节点
	 * @return
	 */
	public String modify(){
		String forward = "";
		
		char state;
		try {
			state = sysMenuService.modifySysMenu(sysMenu);
			
			char status1 = sysMenuService.modifyJuniorFunc(sysMenu, menuCode);
			if(state == '1' && status1 == '1'){
				info = getText("menu.modify.success");
				flag = 1;
				
				forward = "modify_success";
			}else if(state == '2'){
				
				info = getText("menu.code");
				forward = INPUT;
				
			}else if(state == '3'){
				
				info =  getText("menu.name");
				forward = INPUT;
			}else if(state == '4'){
				
				info =  getText("menu.nameEn");
				forward = INPUT;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			forward = INPUT;
		}
		
		
		
		return forward;
	}
	
	/**
	 * 删除一级节点
	 * 
	 * @return
	 */
	public String delete(){
		String forward = "";
		
		if(sysMenu != null){
			sysMenu = sysMenuService.findSysMenuById(sysMenu.getMenuId());
			char state;
			try {
				state = sysMenuService.deleteSysMenu(sysMenu);
				
				
				if(state == '1'){
					info = getText("menu.delete.success");
					flag = 1;
					forward = "delete_success";
				}else {
					
					info = getText("menu.delete.fail");
					forward = "delete_success";
					
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				info = getText("menu.delete.fail");
				forward = "delete_fail";
			}
		}else{
			info = getText("menu.delete.fail");
			forward = "delete_fail";
		}
		return forward;
	}
	
	/**
	 * 管理一级节点下的子菜单节点页面
	 * @return
	 */
	public String manageFunc(){
		sysMenu = sysMenuService.findSysMenuById(sysMenu.getMenuId());
		
		sysFuncs = sysFuncService.sysFuncList(sysMenu.getMenuId());
		
		menuImage = sysMenu.getMenuImage();
		
		menuCode = sysMenu.getMenuCode();
		
		return "manageFunc";
	}
	
	public SysMenu getSysMenu() {
		return sysMenu;
	}
	public void setSysMenu(SysMenu sysMenu) {
		this.sysMenu = sysMenu;
	}
	public SysFunc getSysFunc() {
		return sysFunc;
	}
	public void setSysFunc(SysFunc sysFunc) {
		this.sysFunc = sysFunc;
	}
	public List<SysMenu> getSysMenus() {
		return sysMenus;
	}
	public void setSysMenus(List<SysMenu> sysMenus) {
		this.sysMenus = sysMenus;
	}
	public List<SysFunc> getSysFuncs() {
		return sysFuncs;
	}
	public void setSysFuncs(List<SysFunc> sysFuncs) {
		this.sysFuncs = sysFuncs;
	}
	public List<SysMenuNode> getSysMenuNodes() {
		return sysMenuNodes;
	}
	public void setSysMenuNodes(List<SysMenuNode> sysMenuNodes) {
		this.sysMenuNodes = sysMenuNodes;
	}
	public void setSysMenuService(SysMenuService sysMenuService) {
		this.sysMenuService = sysMenuService;
	}
	public void setSysFuncService(SysFuncService sysFuncService) {
		this.sysFuncService = sysFuncService;
	}

	public String getMenuCode() {
		return menuCode;
	}

	public void setMenuCode(String menuCode) {
		this.menuCode = menuCode;
	}

	public String getMenuImage() {
		return menuImage;
	}

	public void setMenuImage(String menuImage) {
		this.menuImage = menuImage;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}
	
}
